home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / directx / dxf / samples / multimedia / direct3d / bumpmapping / bumpearth / bumpearth.cpp next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  30.7 KB  |  831 lines

  1. //-----------------------------------------------------------------------------
  2. // File: BumpEarth.cpp
  3. //
  4. // Desc: Direct3D environment mapping / bump mapping sample. The technique
  5. //       used perturbs the environment map to simulate bump mapping.
  6. //
  7. //
  8. // Copyright (c) 1997-2000 Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <D3DX8.h>
  14. #include "D3DApp.h"
  15. #include "D3DFont.h"
  16. #include "D3DUtil.h"
  17. #include "DXUtil.h"
  18. #include "resource.h"
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Defines, constants, and global variables
  25. //-----------------------------------------------------------------------------
  26.  
  27. // Vertex with 2nd set of tex coords (for bumpmapped environment map)
  28. struct BUMPVERTEX
  29. {
  30.     D3DXVECTOR3 p;
  31.     D3DXVECTOR3 n;
  32.     FLOAT       tu1, tv1;
  33.     FLOAT       tu2, tv2;
  34. };
  35.  
  36. #define D3DFVF_BUMPVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX2)
  37.  
  38. // Converts a FLOAT to a DWORD for use in SetRenderState() calls
  39. inline DWORD F2DW( FLOAT f ) { return *((DWORD*)&f); }
  40.  
  41.  
  42.  
  43.  
  44. //-----------------------------------------------------------------------------
  45. // Name: class CMyD3DApplication
  46. // Desc: Application class. The base class (CD3DApplication) provides the 
  47. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  48. //       adds functionality specific to this sample program.
  49. //-----------------------------------------------------------------------------
  50. class CMyD3DApplication : public CD3DApplication
  51. {
  52.     CD3DFont*     m_pFont;                  // A font to output text
  53.  
  54.     CD3DArcBall   m_ArcBall;                // ArcBall used for mouse input
  55.  
  56.     LPDIRECT3DTEXTURE8 m_pBlockTexture;     // A blank, gray texture
  57.     LPDIRECT3DTEXTURE8 m_pEarthTexture;     // The Earth texture
  58.     LPDIRECT3DTEXTURE8 m_pEnvMapTexture;    // The environment map
  59.     LPDIRECT3DTEXTURE8 m_pEarthBumpTexture; // Source for the bumpmap
  60.     LPDIRECT3DTEXTURE8 m_psBumpMap;         // The actual bumpmap
  61.  
  62.     D3DFORMAT     m_BumpMapFormat;         // Bumpmap texture format
  63.     LPDIRECT3DVERTEXBUFFER8 m_pEarthVB;    // Geometry for the Earth
  64.     DWORD         m_dwNumSphereVertices;
  65.  
  66.     BOOL          m_bHighTesselation;      // User options
  67.     BOOL          m_bTextureOn;
  68.     BOOL          m_bBumpMapOn;
  69.     BOOL          m_bEnvMapOn;
  70.     BOOL          m_bDeviceValidationFailed;
  71.  
  72.     // Internal functions
  73.     VOID    SetMenuStates();
  74.     HRESULT CreateEarthVertexBuffer();
  75.     VOID    ApplyEnvironmentMap();
  76.     HRESULT InitBumpMap();
  77.  
  78. protected:
  79.     HRESULT OneTimeSceneInit();
  80.     HRESULT InitDeviceObjects();
  81.     HRESULT RestoreDeviceObjects();
  82.     HRESULT InvalidateDeviceObjects();
  83.     HRESULT DeleteDeviceObjects();
  84.     HRESULT Render();
  85.     HRESULT FrameMove();
  86.     HRESULT FinalCleanup();
  87.     HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
  88.  
  89. public:
  90.     CMyD3DApplication();
  91.  
  92.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  93. };
  94.  
  95.  
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Name: WinMain()
  100. // Desc: Entry point to the program. Initializes everything, and goes into a
  101. //       message-processing loop. Idle time is used to render the scene.
  102. //-----------------------------------------------------------------------------
  103. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  104. {
  105.     CMyD3DApplication d3dApp;
  106.  
  107.     if( FAILED( d3dApp.Create( hInst ) ) )
  108.         return 0;
  109.  
  110.     return d3dApp.Run();
  111. }
  112.  
  113.  
  114.  
  115.  
  116. //-----------------------------------------------------------------------------
  117. // Name: CMyD3DApplication()
  118. // Desc: Application constructor. Sets attributes for the app.
  119. //-----------------------------------------------------------------------------
  120. CMyD3DApplication::CMyD3DApplication()
  121. {
  122.     m_strWindowTitle    = _T("BumpEarth: Direct3D BumpMapping Demo");
  123.     m_bUseDepthBuffer   = TRUE;
  124.     m_bShowCursorWhenFullscreen = TRUE;
  125.  
  126.     m_psBumpMap         = NULL;
  127.     m_bTextureOn        = TRUE;
  128.     m_bBumpMapOn        = TRUE;
  129.     m_bEnvMapOn         = TRUE;
  130.     m_bHighTesselation  = TRUE;
  131.  
  132.     m_pBlockTexture     = NULL;
  133.     m_pEarthTexture     = NULL;
  134.     m_pEarthBumpTexture = NULL;
  135.     m_pEnvMapTexture    = NULL;
  136.     m_bDeviceValidationFailed = FALSE;
  137.  
  138.     m_pFont             = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  139.     m_pEarthVB          = NULL;
  140. }
  141.  
  142.  
  143.  
  144.  
  145. //-----------------------------------------------------------------------------
  146. // Name: OneTimeSceneInit()
  147. // Desc: Called during initial app startup, this function performs all the
  148. //       permanent initialization.
  149. //-----------------------------------------------------------------------------
  150. HRESULT CMyD3DApplication::OneTimeSceneInit()
  151. {
  152.     // Set cursor to indicate that user can move the object with the mouse
  153. #ifdef _WIN64
  154.     SetClassLongPtr( m_hWnd, GCLP_HCURSOR, (LONG_PTR)LoadCursor( NULL, IDC_SIZEALL ) );
  155. #else
  156.     SetClassLong( m_hWnd, GCL_HCURSOR, (LONG)LoadCursor( NULL, IDC_SIZEALL ) );
  157. #endif
  158.     return S_OK;
  159. }
  160.  
  161.  
  162.  
  163.  
  164. //-----------------------------------------------------------------------------
  165. // Name: ApplyEnvironmentMap()
  166. // Desc: Performs a calculation on each of the vertices' normals to determine
  167. //       what the texture coordinates should be for the environment map.
  168. //-----------------------------------------------------------------------------
  169. VOID CMyD3DApplication::ApplyEnvironmentMap()
  170. {
  171.     // Get the World-View(WV) matrix set
  172.     D3DXMATRIX matWorld, matView, matWorldView;
  173.     m_pd3dDevice->GetTransform( D3DTS_WORLD, &matWorld );
  174.     m_pd3dDevice->GetTransform( D3DTS_VIEW,  &matView );
  175.     D3DXMatrixMultiply( &matWorldView, &matWorld, &matView );
  176.  
  177.     // Lock the vertex buffer
  178.     BUMPVERTEX* vtx;
  179.     m_pEarthVB->Lock( 0, 0, (BYTE**)&vtx, 0 );
  180.  
  181.     // Establish constants used in sphere generation
  182.     DWORD dwNumSphereRings    = m_bHighTesselation ? 15 :  5;
  183.     DWORD dwNumSphereSegments = m_bHighTesselation ? 30 : 10;
  184.     FLOAT fDeltaRingAngle = ( D3DX_PI / dwNumSphereRings );
  185.     FLOAT fDeltaSegAngle  = ( 2.0f * D3DX_PI / dwNumSphereSegments );
  186.  
  187.     D3DXVECTOR4 vT;
  188.     FLOAT fScale;
  189.  
  190.     // Generate the group of rings for the sphere
  191.     for( DWORD ring = 0; ring < dwNumSphereRings; ring++ )
  192.     {
  193.         FLOAT r0 = sinf( (ring+0) * fDeltaRingAngle );
  194.         FLOAT r1 = sinf( (ring+1) * fDeltaRingAngle );
  195.         FLOAT y0 = cosf( (ring+0) * fDeltaRingAngle );
  196.         FLOAT y1 = cosf( (ring+1) * fDeltaRingAngle );
  197.  
  198.         // Generate the group of segments for the current ring
  199.         for( DWORD seg = 0; seg < (dwNumSphereSegments+1); seg++ )
  200.         {
  201.             FLOAT x0 =  r0 * sinf( seg * fDeltaSegAngle );
  202.             FLOAT z0 =  r0 * cosf( seg * fDeltaSegAngle );
  203.             FLOAT x1 =  r1 * sinf( seg * fDeltaSegAngle );
  204.             FLOAT z1 =  r1 * cosf( seg * fDeltaSegAngle );
  205.  
  206.             // Add two vertices to the strip which makes up the sphere
  207.             // (using the transformed normal to generate texture coords)
  208.             (*vtx).p   = (*vtx).n   = D3DXVECTOR3(x0,y0,z0);
  209.             (*vtx).tu1 = (*vtx).tu2 = -((FLOAT)seg)/dwNumSphereSegments;
  210.             (*vtx).tv1 = (*vtx).tv2 = (ring+0)/(FLOAT)dwNumSphereRings;
  211.             D3DXVec3Transform( &vT, &(*vtx).n, &matWorldView );
  212.             fScale = 1.37f / D3DXVec4Length( &vT );
  213.             (*vtx).tu1 = 0.5f + fScale*vT.x;
  214.             (*vtx).tv1 = 0.5f - fScale*vT.y;
  215.             vtx++;
  216.  
  217.             (*vtx).p   = (*vtx).n   = D3DXVECTOR3(x1,y1,z1);
  218.             (*vtx).tu1 = (*vtx).tu2 = -((FLOAT)seg)/dwNumSphereSegments;
  219.             (*vtx).tv1 = (*vtx).tv2 = (ring+1)/(FLOAT)dwNumSphereRings;
  220.             D3DXVec3Transform( &vT, &(*vtx).n, &matWorldView );
  221.             fScale = 1.37f / D3DXVec4Length( &vT );
  222.             (*vtx).tu1 = 0.5f + fScale*vT.x;
  223.             (*vtx).tv1 = 0.5f - fScale*vT.y;
  224.             vtx++;
  225.         }
  226.     }
  227.  
  228.     m_pEarthVB->Unlock();
  229. }
  230.  
  231.  
  232.  
  233.  
  234. //-----------------------------------------------------------------------------
  235. // Name: FrameMove()
  236. // Desc: Animates the scene
  237. //-----------------------------------------------------------------------------
  238. HRESULT CMyD3DApplication::FrameMove()
  239. {
  240.     // Update the Earth's rotation angle
  241.     static FLOAT fRotationAngle = 0.0f;
  242.     if( FALSE == m_ArcBall.IsBeingDragged() )
  243.         fRotationAngle += m_fElapsedTime;
  244.  
  245.     // Setup viewing postion from ArcBall
  246.     D3DXMATRIX matWorld;
  247.     D3DXMATRIX matRotationInverse;
  248.     D3DXMatrixRotationY( &matWorld, -fRotationAngle );
  249.     D3DXMatrixInverse( &matRotationInverse, NULL, m_ArcBall.GetRotationMatrix() );
  250.     D3DXMatrixMultiply( &matWorld, &matWorld, &matRotationInverse );
  251.     D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetTranslationMatrix() );
  252.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  253.  
  254.     D3DXMATRIX  matView;
  255.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, 0.0f, -3.0f );
  256.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  257.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  258.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  259.     m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
  260.  
  261.     // Apply the environment map
  262.     ApplyEnvironmentMap();
  263.  
  264.     return S_OK;
  265. }
  266.  
  267.  
  268.  
  269.  
  270. //-----------------------------------------------------------------------------
  271. // Name: Render()
  272. // Desc: Renders the scene.
  273. //-----------------------------------------------------------------------------
  274. HRESULT CMyD3DApplication::Render()
  275. {
  276.     DWORD dwNumPasses;
  277.  
  278.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  279.                          0x00000000, 1.0f, 0L );
  280.  
  281.     if( FAILED( m_pd3dDevice->BeginScene() ) )
  282.         return S_OK; // Don't return a "fatal" error
  283.  
  284.     m_pd3dDevice->SetRenderState( D3DRS_WRAP0, D3DWRAP_U | D3DWRAP_V );
  285.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
  286.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  287.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  288.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
  289.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
  290.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
  291.  
  292.     if( m_bTextureOn )
  293.         m_pd3dDevice->SetTexture( 0, m_pEarthTexture );
  294.     else
  295.         m_pd3dDevice->SetTexture( 0, m_pBlockTexture );
  296.  
  297.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 1 );
  298.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  299.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  300.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  301.  
  302.     if( m_bBumpMapOn && m_bEnvMapOn )
  303.     {
  304.         m_pd3dDevice->SetTexture( 1, m_psBumpMap );
  305.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
  306.         if( m_BumpMapFormat == D3DFMT_V8U8 )
  307.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAP );
  308.         else
  309.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAPLUMINANCE );
  310.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  311.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
  312.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT00, F2DW(0.5f) );
  313.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT01, F2DW(0.0f) );
  314.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT10, F2DW(0.0f) );
  315.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT11, F2DW(0.5f) );
  316.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLSCALE, F2DW(4.0f) );
  317.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLOFFSET, F2DW(0.0f) );
  318.  
  319.         if( m_bEnvMapOn )
  320.         {
  321.             m_pd3dDevice->SetTexture( 2, m_pEnvMapTexture );
  322.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0 );
  323.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_ADD );
  324.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  325.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_CURRENT );
  326.         }
  327.         else
  328.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  329.     }
  330.     else
  331.     {
  332.         if( m_bEnvMapOn )
  333.         {
  334.             m_pd3dDevice->SetTexture( 1, m_pEnvMapTexture );
  335.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 );
  336.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_ADD );
  337.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  338.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
  339.         }
  340.         else
  341.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
  342.  
  343.         m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  344.     }
  345.  
  346.     m_pd3dDevice->SetStreamSource( 0, m_pEarthVB, sizeof(BUMPVERTEX) );
  347.     m_pd3dDevice->SetVertexShader( D3DFVF_BUMPVERTEX );
  348.  
  349.     if( FAILED( m_pd3dDevice->ValidateDevice( &dwNumPasses ) ) )
  350.     {
  351.         // The right thing to do when device validation fails is to try
  352.         // a different rendering technique.  This sample just warns the user.
  353.         m_bDeviceValidationFailed = TRUE;
  354.     }
  355.     else
  356.     {
  357.         m_bDeviceValidationFailed = FALSE;
  358.     }
  359.  
  360.     // Finally, draw the Earth
  361.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, m_dwNumSphereVertices-2 );
  362.  
  363.     // Restore texture stage states
  364.     m_pd3dDevice->SetTexture( 0, NULL );
  365.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
  366.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  367.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  368.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  369.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  370.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
  371.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  372.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  373.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  374.  
  375.     // Output statistics
  376.     m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  377.     m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  378.  
  379.     if( m_bDeviceValidationFailed )
  380.     {
  381.         m_pFont->DrawText( 2, 40, D3DCOLOR_ARGB(255,255,0,0), 
  382.             _T("Warning: Device validation failed.  Rendering may not look right.") );
  383.     }
  384.  
  385.     m_pd3dDevice->EndScene();
  386.  
  387.     return S_OK;
  388. }
  389.  
  390.  
  391.  
  392.  
  393. //-----------------------------------------------------------------------------
  394. // Name: InitBumpMap()
  395. // Desc: Copies raw bits from _lpBumpSrc into the type of bump map requested
  396. //       as 'format' into pBumpMap.
  397. //-----------------------------------------------------------------------------
  398. HRESULT CMyD3DApplication::InitBumpMap()
  399. {
  400.     LPDIRECT3DTEXTURE8 psBumpSrc = m_pEarthBumpTexture;
  401.     D3DSURFACE_DESC    d3dsd;
  402.     D3DLOCKED_RECT     d3dlr;
  403.  
  404.     psBumpSrc->GetLevelDesc( 0, &d3dsd );
  405.     // Create the bumpmap's surface and texture objects
  406.     if( FAILED( m_pd3dDevice->CreateTexture( d3dsd.Width, d3dsd.Height, 1, 0, 
  407.         m_BumpMapFormat, D3DPOOL_MANAGED, &m_psBumpMap ) ) )
  408.     {
  409.         return E_FAIL;
  410.     }
  411.  
  412.     // Fill the bits of the new texture surface with bits from
  413.     // a private format.
  414.     psBumpSrc->LockRect( 0, &d3dlr, 0, 0 );
  415.     DWORD dwSrcPitch = (DWORD)d3dlr.Pitch;
  416.     BYTE* pSrc       = (BYTE*)d3dlr.pBits;
  417.  
  418.     m_psBumpMap->LockRect( 0, &d3dlr, 0, 0 );
  419.     DWORD dwDstPitch = (DWORD)d3dlr.Pitch;
  420.     BYTE* pDst       = (BYTE*)d3dlr.pBits;
  421.  
  422.     for( DWORD y=0; y<d3dsd.Height; y++ )
  423.     {
  424.         BYTE* pDstT  = pDst;
  425.         BYTE* pSrcB0 = (BYTE*)pSrc;
  426.         BYTE* pSrcB1 = ( pSrcB0 + dwSrcPitch );
  427.         BYTE* pSrcB2 = ( pSrcB0 - dwSrcPitch );
  428.  
  429.         if( y == d3dsd.Height-1 )  // Don't go past the last line
  430.             pSrcB1 = pSrcB0;
  431.         if( y == 0 )               // Don't go before first line
  432.             pSrcB2 = pSrcB0;
  433.  
  434.         for( DWORD x=0; x<d3dsd.Width; x++ )
  435.         {
  436.             LONG v00 = *(pSrcB0+0); // Get the current pixel
  437.             LONG v01 = *(pSrcB0+4); // and the pixel to the right
  438.             LONG vM1 = *(pSrcB0-4); // and the pixel to the left
  439.             LONG v10 = *(pSrcB1+0); // and the pixel one line below.
  440.             LONG v1M = *(pSrcB2+0); // and the pixel one line above.
  441.  
  442.             LONG iDu = (vM1-v01); // The delta-u bump value
  443.             LONG iDv = (v1M-v10); // The delta-v bump value
  444.  
  445.             if( (v00 < vM1) && (v00 < v01) )  // If we are at valley
  446.             {
  447.                 iDu = vM1-v00;                 // Choose greater of 1st order diffs
  448.                 if( iDu < v00-v01 )
  449.                     iDu = v00-v01;
  450.             }
  451.  
  452.             // The luminance bump value (land masses are less shiny)
  453.             WORD uL = ( v00>1 ) ? 63 : 127;
  454.  
  455.             switch( m_BumpMapFormat )
  456.             {
  457.                 case D3DFMT_V8U8:
  458.                     *pDstT++ = (BYTE)iDu;
  459.                     *pDstT++ = (BYTE)iDv;
  460.                     break;
  461.  
  462.                 case D3DFMT_L6V5U5:
  463.                     *(WORD*)pDstT  = (WORD)( ( (iDu>>3) & 0x1f ) <<  0 );
  464.                     *(WORD*)pDstT |= (WORD)( ( (iDv>>3) & 0x1f ) <<  5 );
  465.                     *(WORD*)pDstT |= (WORD)( ( ( uL>>2) & 0x3f ) << 10 );
  466.                     pDstT += 2;
  467.                     break;
  468.  
  469.                 case D3DFMT_X8L8V8U8:
  470.                     *pDstT++ = (BYTE)iDu;
  471.                     *pDstT++ = (BYTE)iDv;
  472.                     *pDstT++ = (BYTE)uL;
  473.                     *pDstT++ = (BYTE)0L;
  474.                     break;
  475.             }
  476.  
  477.             // Move one pixel to the left (src is 32-bpp)
  478.             pSrcB0+=4;   pSrcB1+=4;   pSrcB2+=4;
  479.         }
  480.  
  481.         // Move to the next line
  482.         pSrc += dwSrcPitch;    pDst += dwDstPitch;
  483.     }
  484.  
  485.     m_psBumpMap->UnlockRect(0);
  486.     psBumpSrc->UnlockRect(0);
  487.  
  488.     return S_OK;
  489. }
  490.  
  491.  
  492.  
  493.  
  494. //-----------------------------------------------------------------------------
  495. // Name: InitDeviceObjects()
  496. // Desc: Initialize scene objects.
  497. //-----------------------------------------------------------------------------
  498. HRESULT CMyD3DApplication::InitDeviceObjects()
  499. {
  500.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  501.  
  502.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Block.bmp"),
  503.                                        &m_pBlockTexture, D3DFMT_R5G6B5 ) ) )
  504.         return D3DAPPERR_MEDIANOTFOUND;
  505.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Earth.bmp"),
  506.                                        &m_pEarthTexture, D3DFMT_R5G6B5 ) ) )
  507.         return D3DAPPERR_MEDIANOTFOUND;
  508.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Earthbump.bmp"),
  509.                                        &m_pEarthBumpTexture, D3DFMT_A8R8G8B8 ) ) )
  510.         return D3DAPPERR_MEDIANOTFOUND;
  511.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("EarthEnvMap.bmp"),
  512.                                        &m_pEnvMapTexture, D3DFMT_R5G6B5 ) ) )
  513.         return D3DAPPERR_MEDIANOTFOUND;
  514.  
  515.     // Find out which bump map texture are supported by this device
  516.     BOOL bCanDoV8U8   = SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
  517.                                    m_d3dCaps.DeviceType, m_d3dsdBackBuffer.Format,
  518.                                    0, D3DRTYPE_TEXTURE,
  519.                                    D3DFMT_V8U8 ) ) &&
  520.                         (m_d3dCaps.TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP );
  521.  
  522.     BOOL bCanDoL6V5U5 = SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
  523.                                    m_d3dCaps.DeviceType, m_d3dsdBackBuffer.Format,
  524.                                    0, D3DRTYPE_TEXTURE,
  525.                                    D3DFMT_L6V5U5 ) ) &&
  526.                         (m_d3dCaps.TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE );
  527.  
  528.     BOOL bCanDoL8V8U8 = SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
  529.                                    m_d3dCaps.DeviceType, m_d3dsdBackBuffer.Format,
  530.                                    0, D3DRTYPE_TEXTURE,
  531.                                    D3DFMT_X8L8V8U8 ) ) &&
  532.                         (m_d3dCaps.TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE );
  533.  
  534.     if( bCanDoV8U8 )        m_BumpMapFormat = D3DFMT_V8U8;
  535.     else if( bCanDoL6V5U5 ) m_BumpMapFormat = D3DFMT_L6V5U5;
  536.     else if( bCanDoL8V8U8 ) m_BumpMapFormat = D3DFMT_X8L8V8U8;
  537.     else                    return E_FAIL;
  538.  
  539.     // Set menu states
  540.     HMENU hMenu = GetMenu( m_hWnd );
  541.     EnableMenuItem( hMenu, IDM_U8V8,   bCanDoV8U8   ? MF_ENABLED : MF_GRAYED );
  542.     EnableMenuItem( hMenu, IDM_U5V5L6, bCanDoL6V5U5 ? MF_ENABLED : MF_GRAYED );
  543.     EnableMenuItem( hMenu, IDM_U8V8L8, bCanDoL8V8U8 ? MF_ENABLED : MF_GRAYED );
  544.     SetMenuStates();
  545.  
  546.     // Initialize earth geometry
  547.     if( FAILED( CreateEarthVertexBuffer() ) )
  548.         return E_FAIL;
  549.  
  550.     // Create and fill the bumpmap
  551.     if( FAILED( InitBumpMap() ) )
  552.         return E_FAIL;
  553.  
  554.     m_bDeviceValidationFailed = FALSE;
  555.  
  556.     return S_OK;
  557. }
  558.  
  559.  
  560.  
  561.  
  562. //-----------------------------------------------------------------------------
  563. // Name: RestoreDeviceObjects()
  564. // Desc: Initialize scene objects.
  565. //-----------------------------------------------------------------------------
  566. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  567. {
  568.     m_pFont->RestoreDeviceObjects();
  569.  
  570.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP );
  571.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP );
  572.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  573.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  574.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP );
  575.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP );
  576.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  577.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  578.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP );
  579.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP );
  580.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  581.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  582.  
  583.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
  584.  
  585.     // Get the aspect ratio
  586.     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
  587.  
  588.     // Set projection matrix
  589.     D3DXMATRIX matProj;
  590.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 0.1f, 25.0f );
  591.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  592.  
  593.     // Set the ArcBall parameters
  594.     m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 3.0f );
  595.     m_ArcBall.SetRadius( 1.0f );
  596.  
  597.     return S_OK;
  598. }
  599.  
  600.  
  601.  
  602.  
  603. //-----------------------------------------------------------------------------
  604. // Name: InvalidateDeviceObjects()
  605. // Desc:
  606. //-----------------------------------------------------------------------------
  607. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  608. {
  609.     m_pFont->InvalidateDeviceObjects();
  610.     return S_OK;
  611. }
  612.  
  613.  
  614.  
  615.  
  616. //-----------------------------------------------------------------------------
  617. // Name: DeleteDeviceObjects()
  618. // Desc: Called when the app is exiting, or the device is being changed,
  619. //       this function deletes any device dependent objects.
  620. //-----------------------------------------------------------------------------
  621. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  622. {
  623.     SAFE_RELEASE( m_pBlockTexture );
  624.     SAFE_RELEASE( m_pEarthTexture );
  625.     SAFE_RELEASE( m_pEarthBumpTexture );
  626.     SAFE_RELEASE( m_pEnvMapTexture );
  627.  
  628.     m_pFont->DeleteDeviceObjects();
  629.  
  630.     SAFE_RELEASE( m_psBumpMap );
  631.     SAFE_RELEASE( m_pEarthVB );
  632.     return S_OK;
  633. }
  634.  
  635.  
  636.  
  637.  
  638. //-----------------------------------------------------------------------------
  639. // Name: FinalCleanup()
  640. // Desc: Called before the app exits, this function gives the app the chance
  641. //       to cleanup after itself.
  642. //-----------------------------------------------------------------------------
  643. HRESULT CMyD3DApplication::FinalCleanup()
  644. {
  645.     SAFE_DELETE( m_pFont );
  646.  
  647.     return S_OK;
  648. }
  649.  
  650.  
  651.  
  652.  
  653. //-----------------------------------------------------------------------------
  654. // Name: ConfirmDevice()
  655. // Desc: Called during device intialization, this code checks the device
  656. //       for some minimum set of capabilities
  657. //-----------------------------------------------------------------------------
  658. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  659.                                           D3DFORMAT Format )
  660. {
  661.     if( dwBehavior & D3DCREATE_PUREDEVICE )
  662.         return E_FAIL; // GetTransform doesn't work on PUREDEVICE
  663.  
  664.     // Device must be able to do bumpmapping
  665.     if( pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE )
  666.     {
  667.         // Accept devices that can create D3DFMT_X8L8V8U8 textures
  668.         if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
  669.                                                   pCaps->DeviceType, Format,
  670.                                                   0, D3DRTYPE_TEXTURE,
  671.                                                   D3DFMT_X8L8V8U8 ) ) )
  672.         {
  673.             return S_OK;
  674.         }
  675.  
  676.         // Accept devices that can create D3DFMT_L6V5U5 textures
  677.         if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
  678.                                                   pCaps->DeviceType, Format,
  679.                                                   0, D3DRTYPE_TEXTURE,
  680.                                                   D3DFMT_L6V5U5 ) ) )
  681.         {
  682.             return S_OK;
  683.         }
  684.     }
  685.  
  686.     if( pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP )
  687.     {
  688.         // Accept devices that can create D3DFMT_V8U8 textures
  689.         if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
  690.                                                   pCaps->DeviceType, Format,
  691.                                                   0, D3DRTYPE_TEXTURE,
  692.                                                   D3DFMT_V8U8 ) ) )
  693.         {
  694.             return S_OK;
  695.         }
  696.     }
  697.  
  698.     // Else, reject the device
  699.     return E_FAIL;
  700. }
  701.  
  702.  
  703.  
  704.  
  705. //-----------------------------------------------------------------------------
  706. // Name: CreateEarthVertexBuffer()
  707. // Desc: Sets up the vertices for a bump-mapped sphere.
  708. //-----------------------------------------------------------------------------
  709. HRESULT CMyD3DApplication::CreateEarthVertexBuffer()
  710. {
  711.     SAFE_RELEASE( m_pEarthVB );
  712.  
  713.     // Choose a tesselation level
  714.     DWORD dwNumSphereRings    = m_bHighTesselation ? 15 :  5;
  715.     DWORD dwNumSphereSegments = m_bHighTesselation ? 30 : 10;
  716.     m_dwNumSphereVertices = 2 * dwNumSphereRings * (dwNumSphereSegments+1);
  717.  
  718.     // Create the vertex buffer
  719.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwNumSphereVertices*sizeof(BUMPVERTEX),
  720.                                                   D3DUSAGE_WRITEONLY, D3DFVF_BUMPVERTEX,
  721.                                                   D3DPOOL_MANAGED, &m_pEarthVB ) ) )
  722.         return E_FAIL;
  723.  
  724.     return S_OK;
  725. }
  726.  
  727.  
  728.  
  729.  
  730. //-----------------------------------------------------------------------------
  731. // Name: SetMenuStates()
  732. // Desc:
  733. //-----------------------------------------------------------------------------
  734. VOID CMyD3DApplication::SetMenuStates()
  735. {
  736.     HMENU hMenu = GetMenu( m_hWnd );
  737.  
  738.     CheckMenuItem( hMenu, IDM_TEXTURETOGGLE,
  739.                     m_bTextureOn ? MF_CHECKED : MF_UNCHECKED );
  740.     CheckMenuItem( hMenu, IDM_BUMPMAPTOGGLE,
  741.                     m_bBumpMapOn ? MF_CHECKED : MF_UNCHECKED );
  742.     CheckMenuItem( hMenu, IDM_ENVMAPTOGGLE,
  743.                     m_bEnvMapOn ? MF_CHECKED : MF_UNCHECKED );
  744.  
  745.     CheckMenuItem( hMenu, IDM_U8V8L8,
  746.                     m_BumpMapFormat==D3DFMT_X8L8V8U8 ? MF_CHECKED : MF_UNCHECKED );
  747.     CheckMenuItem( hMenu, IDM_U5V5L6,
  748.                     m_BumpMapFormat==D3DFMT_L6V5U5 ? MF_CHECKED : MF_UNCHECKED );
  749.     CheckMenuItem( hMenu, IDM_U8V8,
  750.                     m_BumpMapFormat==D3DFMT_V8U8 ? MF_CHECKED : MF_UNCHECKED );
  751.  
  752.     CheckMenuItem( hMenu, IDM_LOW_TESSELATION,
  753.                     (!m_bHighTesselation) ? MF_CHECKED : MF_UNCHECKED );
  754.     CheckMenuItem( hMenu, IDM_HIGH_TESSELATION,
  755.                     m_bHighTesselation ? MF_CHECKED : MF_UNCHECKED );
  756. }
  757.  
  758.  
  759.  
  760.  
  761. //-----------------------------------------------------------------------------
  762. // Name: MsgProc()
  763. // Desc: Message proc function to handle key and menu input
  764. //-----------------------------------------------------------------------------
  765. LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  766.                                     LPARAM lParam )
  767. {
  768.     // Pass mouse messages to the ArcBall so it can build internal matrices
  769.     m_ArcBall.HandleMouseMessages( hWnd, uMsg, wParam, lParam );
  770.  
  771.     // Trap context menu
  772.     if( WM_CONTEXTMENU == uMsg )
  773.         return 0;
  774.  
  775.     // Handle menu commands
  776.     if( WM_COMMAND == uMsg )
  777.     {
  778.         switch( LOWORD(wParam) )
  779.         {
  780.             case IDM_TEXTURETOGGLE:
  781.                 m_bTextureOn = !m_bTextureOn;
  782.                 break;
  783.  
  784.             case IDM_BUMPMAPTOGGLE:
  785.                 m_bBumpMapOn = !m_bBumpMapOn;
  786.                 break;
  787.  
  788.             case IDM_ENVMAPTOGGLE:
  789.                 m_bEnvMapOn  = !m_bEnvMapOn;
  790.                 break;
  791.  
  792.             case IDM_U8V8L8:
  793.                 SAFE_RELEASE( m_psBumpMap );
  794.                 m_BumpMapFormat = D3DFMT_X8L8V8U8;
  795.                 InitBumpMap();
  796.                 break;
  797.  
  798.             case IDM_U5V5L6:
  799.                 SAFE_RELEASE( m_psBumpMap );
  800.                 m_BumpMapFormat = D3DFMT_L6V5U5;
  801.                 InitBumpMap();
  802.                 break;
  803.  
  804.             case IDM_U8V8:
  805.                 SAFE_RELEASE( m_psBumpMap );
  806.                 m_BumpMapFormat = D3DFMT_V8U8;
  807.                 InitBumpMap();
  808.                 break;
  809.  
  810.             case IDM_LOW_TESSELATION:
  811.                 m_bHighTesselation = FALSE;
  812.                 CreateEarthVertexBuffer();
  813.                 break;
  814.  
  815.             case IDM_HIGH_TESSELATION:
  816.                 m_bHighTesselation = TRUE;
  817.                 CreateEarthVertexBuffer();
  818.                 break;
  819.         }
  820.  
  821.         // Update the menus, in case any state changes occurred
  822.         SetMenuStates();
  823.     }
  824.  
  825.     // Pass remaining messages to default handler
  826.     return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
  827. }
  828.  
  829.  
  830.  
  831.